#pragma once
#include"common.hpp"
class InetAddr
{
public:
    InetAddr(){}
    //网络转主机
    InetAddr(sockaddr_in sock)
    {
        //_ip=inet_ntoa(_sockaddrin.sin_addr);

        // _port = ntohs(_addr.sin_port);
        // char ipbuffer[64];
        // inet_ntop(AF_INET, &_addr.sin_addr, ipbuffer, sizeof(_addr));
        // _ip = ipbuffer;

        SetAddr(sock);
    }
    //主机转网络
     InetAddr(const std::string &ip, uint16_t port) : _ip(ip), _port(port)
    {
        // 主机转网络
        memset(&_addr, 0, sizeof(_addr));
        _addr.sin_family = AF_INET;
        inet_pton(AF_INET, _ip.c_str(), &_addr.sin_addr);
        _addr.sin_port = htons(_port);
    }
    InetAddr(uint16_t port) : _port(port), _ip("0")
    {
        // 主机转网络
        memset(&_addr, 0, sizeof(_addr));
        _addr.sin_family = AF_INET;
        _addr.sin_addr.s_addr = INADDR_ANY;
        _addr.sin_port = htons(_port);
    }
    const struct sockaddr *NetAddrPtr()
    {
        return CONV(_addr);
    }
    ~InetAddr()
    {
    }
    std::string Ip()
    {
        return _ip;
    }
    uint16_t Port()
    {
        return _port;
    }
    bool operator==(const InetAddr &peer)
    {
        return _ip == peer._ip && _port == peer._port;
    }
    std::string StringAddr()
    {
        return _ip + ":" + std::to_string(_port);
    }
    socklen_t NetAddrLen()
    {
        return sizeof(_addr);
    }
    const struct sockaddr_in &NetAddr() { return _addr; }
    void SetAddr(sockaddr_in add)
    {
        _addr=add;
        int port=ntohs(_addr.sin_port);
        char ipbuffer[64];
        inet_ntop(AF_INET, &_addr.sin_addr, ipbuffer, sizeof(_addr));
        _ip=ipbuffer;

    }

private:
    sockaddr_in _addr;
    std::string _ip;
    uint16_t _port;
};